from django.shortcuts import render

# Create your views here.


from rest_framework.views import APIView
from rest_framework.response import Response
from .serializer import *
from django.core.paginator import Paginator


class LoginView(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user_obj = UserModel.objects.filter(username=username).first()
        if user_obj:
            if password == user_obj.password:
                return Response({'msg': '登录成功', 'code': 200, 'authority': user_obj.authority})
            else:
                return Response({'msg': '用户或密码不正确', 'code': 400})
        else:
            return Response({'msg': '用户或密码不正确', 'code': 400})


class UserView(APIView):
    def get(self, request, pid):
        user_obj = UserModel.objects.all()
        paginator = Paginator(user_obj, 2)
        paged = paginator.page(pid)
        ser = UserSer(paged, many=True)
        return Response({
            'page_sum': paginator.num_pages,  # 总页数
            'page_list': [i for i in paginator.page_range],
            'data': ser.data
        })
